<?php
function str_prefix($str, $n = 1, $char = " ")
{
    for ($x = 0; $x < $n; $x++) {
        $str = $char . $str;
    }
    return $str;
}

?>

<div class="panel">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>功能点</th>
                    <th>controller id</th>
                    <th>action id</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($items as $item) {
                    $itemName = $item['name'];
//                    $itemName = str_prefix($itemName, $item['level'], '—');
                    if ($item['level'] > 0) {
//                        $itemName = '┗' . $itemName;
//                        $itemName = '— ' . $itemName;
                        $itemName = '● ' . $itemName;
                    }
                    $itemName = str_prefix($itemName, $item['level'], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
                    ?>
                    <tr>
                        <td><?= $itemName ?></td>
                        <td><?= $item['controller_id'] ?></td>
                        <td><?= $item['action_id'] ?></td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-link" data-toggle="modal" data-id="<?= $item['id'] ?>" data-name="<?= $item['name'] ?>" href="#addModal">添加子功能点</a>
                                <!--                                <a class="btn btn-link" data-toggle="modal" data-id="--><?//= $item['id'] ?><!--" href="#detailModal">详情</a>-->
                                <?php
                                if ($item['id'] != '0') {
                                    ?>
                                    <button class="btn btn-link btn-delete-function-point" data-id="<?= $item['id'] ?>">删除</button>
                                    <button class="btn btn-link btn-edit-function-point" data-id="<?= $item['id'] ?>">编辑</button>
                                    <?php
                                }
                                ?>
                            </div>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="addModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title" id="txtAddModalTitle">添加子功能点</h4>
            </div>
            <div class="modal-body">
                <div class="form-horizontal">
                    <input type="hidden" id="inputId">
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">父功能点:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputParentName" disabled/>
                            <input type="hidden" id="inputParentId"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">名称:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputName"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label col-lg-3">描述:</label>
                        <div class="col-lg-9">
                            <textarea rows="2" class="form-control" id="inputDescription"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">controller id:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputControllerId"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">action id:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputActionId"/>
                        </div>
                    </div>

                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
                <button class="btn btn-primary" id="btnAddFunctionPointOk">确定</button>
            </div>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="editModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">编辑子功能点</h4>
            </div>
            <div class="modal-body">
                <div class="form-horizontal">
                    <input type="hidden" id="inputEditId">
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">父功能点:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputEditParentName" disabled/>
                            <input type="hidden" id="inputEditParentId"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">名称:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputEditName"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label col-lg-3">描述:</label>
                        <div class="col-lg-9">
                            <textarea rows="2" class="form-control" id="inputEditDescription"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">controller id:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputEditControllerId"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 col-sm-3 control-label">action id:</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="inputEditActionId"/>
                        </div>
                    </div>

                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
                <button class="btn btn-primary" id="btnEditFunctionPointOk">确定</button>
            </div>
        </div>
    </div>
</div>

<script type="application/javascript">
    (function () {
        $('#btnAddFunctionPointOk').click(function () {
            var data = {};
            data.id = '';
            data.name = $('#inputName').val().trim();
            data.description = $('#inputDescription').val().trim();
            data.controller_id = $('#inputControllerId').val().trim();
            data.action_id = $('#inputActionId').val().trim();
            data.parent_id = $('#inputParentId').val().trim();
            requestJson('add-function-point-handler', data, true, function (responseData) {
                if (responseData.status == 0) {
                    window.location.reload();
                } else {
                    alert(responseData.message);
                }
            });
        });

        $('#btnEditFunctionPointOk').click(function () {
            var data = {};
            data.id = $('#inputEditId').val().trim();
            data.name = $('#inputEditName').val().trim();
            data.description = $('#inputEditDescription').val().trim();
            data.controller_id = $('#inputEditControllerId').val().trim();
            data.action_id = $('#inputEditActionId').val().trim();
            data.parent_id = $('#inputEditParentId').val().trim();
            requestJson('edit-function-point-handler', data, true, function (responseData) {
                if (responseData.status == 0) {
                    window.location.reload();
                } else {
                    alert(responseData.message);
                }
            });
        });

        $('#addModal').on('show.bs.modal', function (e) {
            $("#inputParentName").val(e.relatedTarget.attributes['data-name'].value);
            $("#inputParentId").val(e.relatedTarget.attributes['data-id'].value);
        });

        $('.btn-delete-function-point').click(function () {
            var result = confirm('是否确认删除此功能点?');
            if (result == true) {
                var functionPointId = $(this).attr('data-id');
                requestJson('delete-function-point-handler', {'function_point_id': functionPointId}, true, function (responseData) {
                    if (responseData.status == 0) {
                        window.location.reload();
                    } else {
                        alert(responseData.message);
                    }
                });
            }
        });

        $('.btn-edit-function-point').click(function () {
            var id = $(this).data('id');
            requestJson('get-function-point-handler', {'id': id}, true, function (responseData) {
                if (responseData.status == 0) {
                    $('#editModal').modal('show');
                    $('#inputEditId').val(responseData.data.id);
                    $('#inputEditParentName').val(responseData.data.parent_name);
                    $('#inputEditParentId').val(responseData.data.parent_id);
                    $('#inputEditName').val(responseData.data.name);
                    $('#inputEditDescription').val(responseData.data.description);
                    $('#inputEditControllerId').val(responseData.data.controller_id);
                    $('#inputEditActionId').val(responseData.data.action_id);
                } else {
                    alert(responseData.message);
                }
            });
        });
    })(jQuery);

    //设置模态窗:当用户点击模态框外部时不会关闭、当初始化时不显示模态框。
    $(function () {
        $('#addModal').modal({
            backdrop: 'static', show: false
        });
        $('#editModal').modal({
            backdrop: 'static', show: false
        });
    });
</script>

